<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
	public $name = 'User';
	public $hasMany = 'Article';
	public $belongsTo = array(
		'Role' => array(
			'counterCache' => true,

		)
	);

	public $validate = array(
		'username' => array(
			'required1' => array(
				'rule' => 'notEmpty',
				'message' => 'Please enter a username'
			),
			'required2' => array(
				'rule' => 'isUnique',
				'message' => 'This username already exists'
			)
		),
		'email' => array(
			'required1' => array(
				'rule' => 'email',
				'message' => 'Please enter a valid email address'
			),
			'required2' => array(
				'rule' => 'isUnique',
				'message' => 'This email address has already been registered'
			)
		),
		'password' => array(
			'rule' => 'notEmpty',
		)
	);

	public function beforeSave() {
		if (isset($this->data[$this->alias]['password'])) {
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		}
		return true;
	}

	public function beforeValidate(){
		if (strlen($this->data[$this->alias]['password']) < 1) {
			unset($this->data[$this->alias]['password']);
		}
		return true;
	}

}